define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var group_id=$('#group_id').val();
    var dragsort_url='ajax/weigh';
    console.log(group_id);
    var isadd=true;
    var isstatus=true;
    var isshow_status=false;
    if(group_id==7||group_id==6){
        dragsort_url='';
        isadd=false;
    }
    if(group_id==7){
        isstatus=false;
        isshow_status=true;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'avideo/index' + location.search,
                    add_url: 'avideo/add',
                    edit_url: 'avideo/edit',
                    del_url: 'avideo/del',
                    multi_url: 'avideo/multi',
                    import_url: 'avideo/import',
                    table: 'avideo',
                    dragsort_url:dragsort_url,
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                sortOrder:'asc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status,
                        //     visible:isstatus
                        // },
                        {field: 'show_status', title: __('show_status'),table: table,formatter: Table.api.formatter.buttons,buttons:[
                                {
                                    name: 'detail',
                                    text: '显示视频',
                                    title: '显示视频',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-address-book-o',
                                    url: 'avideo/showcheck',
                                    confirm:'显示这个视频,确定吗?',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        $(".btn-refresh").trigger('click')
                                        return false;
                                    },
                                    visible: function (row) {
                                        if (row.show_status==='0'){
                                            //返回true时按钮显示,返回false隐藏
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '隐藏视频',
                                    title: '隐藏视频',
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    icon: 'fa fa-address-book-o',
                                    url: 'avideo/hiddencheck',
                                    confirm:'隐藏这个视频,确定吗?',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        $(".btn-refresh").trigger('click')
                                        return false;
                                    },
                                    visible: function (row) {
                                        if (row.show_status==='1'){
                                            //返回true时按钮显示,返回false隐藏
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                }
                            ],visible:isshow_status
                        },
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons:[

                                {
                                    name: 'detail',
                                    text: '查看相关阅读',
                                    title: '查看相关阅读',
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-address-book-o',
                                    url:function(row){
                                        return 'article/index?ids='+row.id
                                    },
                                },
                                {
                                    name: 'detail',
                                    text: '查看阅读文字',
                                    title: '查看阅读文字',
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    icon: 'fa fa-address-book-o',
                                    url:function(row){
                                        return 'article_word/index?ids='+row.id
                                    },
                                },
                                {
                                    name: 'detail',
                                    text: '查看对应考试',
                                    title: '查看对应考试',
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    icon: 'fa fa-address-book-o',
                                    url:function(row){
                                        return 'exam/index?ids='+row.id
                                    },
                                },
                                // {
                                //     name: 'detail',
                                //     text: '提交申请',
                                //     title: '提交申请',
                                //     classname: 'btn btn-xs btn-default btn-ajax',
                                //     icon: 'fa fa-address-book-o',
                                //     url: 'avideo/applycheck',
                                //     confirm:'提交申请,确定吗?',
                                //     success: function (data, ret) {
                                //         Layer.alert(ret.msg);
                                //         $(".btn-refresh").trigger('click')
                                //         return false;
                                //     },
                                //     visible: function (row) {
                                //         if ( row.status==='0'){
                                //             //返回true时按钮显示,返回false隐藏
                                //             return true;
                                //         }else{
                                //             return false;
                                //         }
                                //     }
                                // },
                                // {
                                //     name: 'detail',
                                //     text: '重新提交申请',
                                //     title: '重新提交申请',
                                //     classname: 'btn btn-xs btn-default btn-ajax',
                                //     icon: 'fa fa-address-book-o',
                                //     url: 'avideo/applycheck',
                                //     confirm:'重新提交申请,确定吗?',
                                //     success: function (data, ret) {
                                //         Layer.alert(ret.msg);
                                //         $(".btn-refresh").trigger('click')
                                //         return false;
                                //     },
                                //     visible: function (row) {
                                //         if ( row.group_id===9&&row.status==='2'){
                                //             //返回true时按钮显示,返回false隐藏
                                //             return true;
                                //         }else{
                                //             return false;
                                //         }
                                //     }
                                // },
                                // {
                                //     name: 'detail',
                                //     text: '中级审批通过',
                                //     title: '中级审批通过',
                                //     classname: 'btn btn-xs btn-danger btn-ajax',
                                //     icon: 'fa fa-address-book-o',
                                //     url: 'avideo/midcheck',
                                //     confirm:'中级审批通过,确定吗?',
                                //     success: function (data, ret) {
                                //         Layer.alert(ret.msg);
                                //         $(".btn-refresh").trigger('click')
                                //         return false;
                                //     },
                                //     visible: function (row) {
                                //         if ((row.group_id===6)&&(row.status==='1'||row.status==='2')||row.group_id===1){
                                //             //返回true时按钮显示,返回false隐藏
                                //             return true;
                                //         }else{
                                //             return false;
                                //         }
                                //     }
                                // },
                                // {
                                //     name: 'detail',
                                //     text: '中级审批拒绝',
                                //     title: '中级审批拒绝',
                                //     classname: 'btn btn-xs btn-danger btn-ajax',
                                //     icon: 'fa fa-address-book-o',
                                //     url: 'avideo/nomidcheck',
                                //     confirm:'中级审批拒绝,确定吗?',
                                //     success: function (data, ret) {
                                //         Layer.alert(ret.msg);
                                //         $(".btn-refresh").trigger('click')
                                //         return false;
                                //     },
                                //     visible: function (row) {
                                //         if ((row.group_id===6)&&(row.status==='1'||row.status==='2')||row.group_id===1){
                                //             //返回true时按钮显示,返回false隐藏
                                //             return true;
                                //         }else{
                                //             return false;
                                //         }
                                //     }
                                // }
                            ],
                            events: Table.api.events.operate, formatter: function(value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (row.group_id===7||row.group_id===6||(row.group_id===9&&row.status>2)) {
                                    $(table).data("operate-del", null);
                                    $(table).data("operate-edit", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                        // {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'operate', title: '添加视频资料', table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                        //         return '<a href="avfile/add?ids='+ row["id"] + '" class="btn btn-xs btn-warning btn-dialog" title="添加视频资料" > <i class="fa fa-users"></i> </a>';
                        //     }
                        // },
                        {field: 'operate', title: '添加相关阅读', table: table,events: Table.api.events.operate, formatter: function (value, row, index) {
                                if (row.group_id===7||row.group_id===6||(row.group_id===9&&row.status>2)){
                                    return '';
                                }else{
                                    return '<a href="article/add?ids='+ row["id"] + '" class="btn btn-xs btn-warning btn-dialog" title="添加相关阅读" > <i class="fa fa-users"></i> </a>';
                                }
                            },visible:isadd
                        },
                        {field: 'operate', title: '添加文字阅读', table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                                if (row.group_id===7||row.group_id===6||(row.group_id===9&&row.status>2)){
                                    return '';
                                }else{
                                    return '<a href="article_word/add?ids='+ row["id"] + '" class="btn btn-xs btn-success btn-dialog" title="添加文字阅读" > <i class="fa fa-book"></i> </a>';
                                }
                            },visible:isadd
                        },
                        {field: 'operate', title: '添加视频的考试', table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                                if (row.group_id===7||row.group_id===6||(row.group_id===9&&row.status>2)){
                                    return '';
                                }else{
                                    return '<a href="exam/add?ids='+ row["id"] + '" class="btn btn-xs btn-warning btn-dialog" title="添加视频考试信息" > <i class="fa fa-book"></i> </a>';
                                }
                            },visible:isadd
                        },

                        {field: 'name', title: __('Title'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        //{field: 'cate_id', title: __('Cate_id')},
                        {field: 'ji_fen', title: __('Ji_fen')},
                        {field: 'xue_fen', title: __('Xue_fen')},
                        {field: 'srcfile', title: __('Src'), operate: false,formatter: function (value, row, index) {
                                return '<a href="'+ row["srcfile"] + '" target="_blank" class="btn btn-xs btn-warning" >打开视频</a>';
                            }
                        },
                        {field: 'col.name', title: __('Col.name'), operate: 'LIKE'},

                        //{field: 'period_id', title: __('Period_id')},
                        {field: 'period.name', title: "视频所属学习阶段", operate: 'LIKE'},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'intro', title: __('Intro'), operate: 'LIKE'},
                        {field: 'category.name', title: "视频所属部门", operate: 'LIKE'},
                        {field: 'ctime', title: __('Ctime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false,  formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        {field: 'lasttime', title: '截止时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false,  formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        {field: 'comments', title: __('Comments')},
                        {field: 'shows', title: __('Shows')},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
        },
        add: function () {
            $("#c-cate_id").data("params", function (obj) {
                return {pid: $("#c-company_id").val()};
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-cate_id").data("params", function (obj) {
                return {pid: $("#c-company_id").val()};
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});